package com.yangmf.auto_message_handle.controller.user;

import com.yangmf.auto_message_handle.content.BaseContext;
import com.yangmf.auto_message_handle.pojo.dto.EventqueryDTO;
import com.yangmf.auto_message_handle.pojo.vo.UserVO;
import com.yangmf.auto_message_handle.result.Result;
import com.yangmf.auto_message_handle.service.EventService;
import com.yangmf.auto_message_handle.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Max;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/user/event")
@Tag(name = "数据模块")
@Slf4j
public class EventController {

    @Autowired
    private EventService eventService;

    @Autowired
    private UserService userService;

    @Operation(summary = "获取事件列表")
    @GetMapping("/list")
    public Result<Object> list(@RequestParam(required = false) String eventId,
                               @RequestParam(required = false) String eventName,
                               @RequestParam(required = false)
                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime createTime,
                               @RequestParam(required = false) String eventType,
                               @RequestParam(required = false) String eventSubject,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(defaultValue = "1") Integer pageNum
                               ){
        log.info("当前用户id{}", BaseContext.getCurrentId());
        log.info("获取分页参数页数{}，大小{}",pageNum,pageSize);
        if(createTime==null){
            createTime = LocalDateTime.now().minusDays(1);

        }
        EventqueryDTO queryDTO = EventqueryDTO.builder()
                .eventId(eventId)
                .eventName(eventName)
                .createTime(createTime)
                .eventType(eventType)
                .eventSubject(eventSubject)
                .pageNum(pageNum)
                .pageSize(pageSize)
                .build();



        return Result.success( eventService.list(queryDTO));
    }
    @Operation(summary = "获取事件详情")
    @GetMapping("/detail")
    public Result getdetailEvent(Long id)
    {
        return Result.success(eventService.getEventById(id));
    }



    @Operation(summary = "批量删除事件")
    @DeleteMapping("/batchDelete")
    public Result batchDeleteEvents(@RequestBody List<Long> ids) {
        UserVO userInfo = userService.getUserInfo();
        if (userInfo.getCharacter()==0) {
            return Result.error("无权限");
        }
        log.info("有权限删除");
        log.info("批量删除事件，事件ID列表: {}", ids);
        try {
            eventService.batchDeleteEvents(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            log.error("批量删除事件失败", e);
            return Result.error("批量删除失败: " + e.getMessage());
        }
    }
    @Operation(summary = "将事件1归入事件2")
    @PostMapping("/merge")
    public Result mergeEvent(Long eventId1, Long eventId2) {
        UserVO userInfo = userService.getUserInfo();
        if (userInfo.getCharacter()==0) {
            return Result.error("无权限");
        }
        log.info("有权限");
        eventService.mergeEvent(eventId1, eventId2);
        return Result.success("将事件"+eventId1+"归入事件"+eventId2+"成功");
    }

    @Operation(summary = "归档事件")
    @PostMapping("/archive")
    public Result archiveEvent(Long eventId,Long event_Type) {
        UserVO userInfo = userService.getUserInfo();
        if (userInfo.getCharacter()==0) {
            return Result.error("无权限");
        }
        log.info("有权限");
        eventService.archiveEvent(eventId,event_Type);
        return Result.success("归档事件"+eventId+"成功");
    }


}